package com.zbkj.service.service.monitor;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.zbkj.common.model.monitor.ZfbMonitorEquipment;
import com.zbkj.common.monitorchannel.vo.MonitorChannelMediaAddressVo;
import com.zbkj.common.response.ProductInfoResponse;
import com.zbkj.service.monitor.MonitorChannelHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ProductMonitorService
 * @Description: TODO
 * @Author: fanbing
 * @Date: 2024/6/29 18:40
 * @Version: V1.0
 **/
@Service
@Slf4j
public class BrandProductMonitorService {

    @Autowired
    private MonitorChannelHandle monitorChannelHandle;

    @Autowired
    private IZfbMonitorEquipmentService zfbMonitorEquipmentService;


    public ProductInfoResponse fillProductDetailResponseMediaAddress(ProductInfoResponse response) {
            long startTime = System.currentTimeMillis();
            if (ObjectUtil.isNull(response.getLicense())) {
                response.setLicense(new ArrayList<>());
            }
            List<MonitorChannelMediaAddressVo> mediaAddressList = Lists.newArrayList();
            if (ObjectUtil.isNotNull(response.getLicense())) {
                response.getLicense().forEach(license -> {
                    ZfbMonitorEquipment monitorEquipment = zfbMonitorEquipmentService.getOne(
                            new LambdaQueryWrapper<ZfbMonitorEquipment>().eq(ZfbMonitorEquipment::getLicense, license)
                    );
                    if (ObjectUtil.isNull(monitorEquipment)) {
                        log.warn("未找到对应的监控设备，license: {}", license);
                        return;
                    }
//                if (ObjectUtil.isAnyNull(monitorEquipment.getLicense(), monitorEquipment.getEquipmentType())) {
//                    log.warn("监控设备信息不完整，license: {}, equipmentType: {}",
//                            monitorEquipment.getLicense(), monitorEquipment.getEquipmentType());
//                    return;
//                }
                    MonitorChannelMediaAddressVo mediaAddress = monitorChannelHandle.getMediaAddress(
                            monitorEquipment.getLicense(),
                            monitorEquipment.getEquipmentType()
                    );
                    if (ObjectUtil.isNotNull(mediaAddress)) {
                        mediaAddressList.add(mediaAddress);
                    } else {
                        log.warn("获取媒体地址失败，license: {}, equipmentType: {}",
                                monitorEquipment.getLicense(), monitorEquipment.getEquipmentType());
                    }

                    log.info("#fillProductDetailResponseMediaAddress#监控流媒体地址耗时：{}", System.currentTimeMillis() - startTime);
                });
            }
//        response.getLicense().forEach(license -> {
//            ZfbMonitorEquipment monitorEquipment = zfbMonitorEquipmentService.getOne(new LambdaQueryWrapper<ZfbMonitorEquipment>().eq(ZfbMonitorEquipment::getLicense,license));
//            if (ObjectUtil.isNotNull(monitorEquipment)) {
//                MonitorChannelMediaAddressVo mediaAddress = monitorChannelHandle.getMediaAddress(monitorEquipment.getLicense(), monitorEquipment.getEquipmentType());
//                mediaAddressList.add(mediaAddress);
//                log.info("#fillProductDetailResponseMediaAddress#监控流媒体地址耗时：{}", System.currentTimeMillis() - startTime);
//            }
//        });
            response.setMonitorMediaAddressList(mediaAddressList);
            return response;
    }
}
